package org.lb.MarketingSystem.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lb.MarketingSystem.dao.CreateTableMapperDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class UrlInterceptor implements HandlerInterceptor{
	//导入创建表dao
	@Autowired
	CreateTableMapperDao createTableDao;
	//是否创建表单状态位
	static boolean CREATEFLAG=true;

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		request.setCharacterEncoding("utf-8");
		//创建表单状态位
		if(CREATEFLAG){
			//调用创建表单方法
			new CreateTable().createTableMethod(createTableDao);
			System.out.println("创建数据库表单完成");
			CREATEFLAG=false;
		}
		String url=request.getServletPath();
//		if(url.contains("system")){
//			Object user = request.getSession().getAttribute("user");
//			if(user!=null||"/system/staff_login2".equals(url)){
//				return true;
//			}else{
//				request.getRequestDispatcher("/WEB-INF/jsp/staff_login.jsp").forward(request, response);
//				return true;
//			}
//		}
//	    if(url.contains("customer")){
//	    	Object user = request.getAttribute("customerUser");
//	    	if(user!=null||"/customer/customerLogin".equals(url)){
//	    		return true;
//	    	}else{
//				request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
//	    		return true;
//	    	}
//	    }
		return true;
	}

}
